home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / minix / up1510b.tgz / up1510b / src / commands / ic / ic_input.c < prev    next >
C/C++ Source or Header  |  1990-07-23  |  5KB  |  278 lines

  1. /****************************************************************/
  2. /*                                */
  3. /*    ic_input.c                        */
  4. /*                                */
  5. /*        Character input routines for the        */
  6. /*        "Integer Calculator".                */
  7. /*                                */
  8. /****************************************************************/
  9. /*  origination          1988-Apr-7         Terrence W. Holm    */
  10. /*  added cmd line args  1988-May-13        Terrence W. Holm    */
  11. /****************************************************************/
  12.  
  13.  
  14.  
  15. #include <stdio.h>
  16. #include <ctype.h>
  17. #include <sgtty.h>
  18.  
  19. #include "ic.h"
  20.  
  21. static struct sgttyb saved_mode;
  22. static struct tchars saved_chars;
  23.  
  24.  
  25.  
  26.  
  27. /****************************************************************/
  28. /*                                */
  29. /*    Save_Term()                        */
  30. /*                                */
  31. /*        Save the current terminal characteristics.    */
  32. /*                                */
  33. /****************************************************************/
  34.  
  35.  
  36. Save_Term()
  37.  
  38.   {
  39.   ioctl( 0, TIOCGETP, &saved_mode  );
  40.   ioctl( 0, TIOCGETC, (struct sgttyb *) &saved_chars );
  41.   }
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50. /****************************************************************/
  51. /*                                */
  52. /*    Set_Term()                        */
  53. /*                                */
  54. /*        Set up the terminal characteristics for ic.    */
  55. /*                                */
  56. /****************************************************************/
  57.  
  58.  
  59. Set_Term()
  60.  
  61.   {
  62.   struct sgttyb ic_mode;
  63.   struct tchars ic_chars;
  64.  
  65.   ic_mode  = saved_mode;
  66.   ic_chars = saved_chars;
  67.  
  68.  
  69.   /*  No tab expansion, no echo, cbreak mode            */
  70.  
  71.   ic_mode.sg_flags = ic_mode.sg_flags & ~XTABS & ~ECHO  |  CBREAK;
  72.  
  73.  
  74.   /*  Change the interrupt character to ^C, ignore ^S & ^Q     */
  75.  
  76.   ic_chars.t_intrc  = '\003';
  77.   ic_chars.t_startc = '\377';
  78.   ic_chars.t_stopc  = '\377';
  79.  
  80.   ioctl( 0, TIOCSETP, &ic_mode  );
  81.   ioctl( 0, TIOCSETC, (struct sgttyb *) &ic_chars );
  82.   }
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91. /****************************************************************/
  92. /*                                */
  93. /*    Reset_Term()                        */
  94. /*                                */
  95. /*        Restore the terminal characteristics.        */
  96. /*                                */
  97. /****************************************************************/
  98.  
  99.  
  100. Reset_Term()
  101.  
  102.   {
  103.   ioctl( 0, TIOCSETP, &saved_mode  );
  104.   ioctl( 0, TIOCSETC, (struct sgttyb *) &saved_chars );
  105.   }
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114. /****************************************************************/
  115. /*                                */
  116. /*    Get_Char()                        */
  117. /*                                */
  118. /*        Return the next input character. Upper case    */
  119. /*        is mapped to lower case. Escape sequences    */
  120. /*        are mapped to special codes (msb set).        */
  121. /*                                */
  122. /****************************************************************/
  123.  
  124.  
  125. int Get_Char()
  126.  
  127.   {
  128.   int c;
  129.  
  130.  
  131.   /*  fflush() used because Minix does not automatically    */
  132.   /*  flush the output.                        */
  133.  
  134.   fflush( stdout );
  135.  
  136.  
  137.   if ( (c = Getc()) == EOF )
  138.     return( EOF );
  139.   
  140.   c &= 0x7f;
  141.  
  142.  
  143.   if ( isupper(c) )
  144.     return( tolower(c) );
  145.  
  146.  
  147.   if ( c == ESCAPE )
  148.     if ( (c=Getc()) != '[' )
  149.       {
  150.       ungetc( c, stdin );
  151.       return( ESCAPE );
  152.       }
  153.     else
  154.       {
  155.       c = Getc() | 0x80;
  156.  
  157.       if (  c == ESC_HOME  ||  c == ESC_UP    ||  c == ESC_PGUP   ||
  158.         c == ESC_LEFT  ||  c == ESC_5     ||  c == ESC_RIGHT  ||
  159.         c == ESC_END   ||  c == ESC_DOWN  ||  c == ESC_PGDN   ||
  160.         c == ESC_PLUS  ||  c == ESC_MINUS   )
  161.     return( c );
  162.       else
  163.     return( ESCAPE );
  164.       }
  165.  
  166.  
  167.   return( c );
  168.   }
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177. /****************************************************************/
  178. /*                                */
  179. /*    Init_Getc( argc, argv )                    */
  180. /*                                */
  181. /*        Give Getc() references to the command line    */
  182. /*        arguments.                    */
  183. /*                                */
  184. /****************************************************************/
  185.  
  186.  
  187.  
  188. static int    args_remaining;
  189. static char **args_pointer;
  190. static int    args_index;
  191.  
  192.  
  193.  
  194. Init_Getc( argc, argv )
  195.   int   argc;
  196.   char *argv[];
  197.  
  198.   {
  199.   args_remaining = argc - 1;
  200.   args_pointer   = &argv[1];
  201.   args_index     = 0;
  202.   }
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211. /****************************************************************/
  212. /*                                */
  213. /*    Getc()                            */
  214. /*                                */
  215. /*        Get the next input character from the command    */
  216. /*        line if there is some more, else from stdin.    */
  217. /*                                */
  218. /****************************************************************/
  219.  
  220.  
  221. int Getc()
  222.  
  223.   {
  224.   int c;
  225.  
  226.   if ( args_remaining > 0 )
  227.     if ( (c = args_pointer[ 0 ][ args_index++ ]) == '\0' )
  228.       {
  229.       --args_remaining;
  230.       ++args_pointer;
  231.       args_index = 0;
  232.  
  233.       if ( args_remaining > 0 )
  234.         return( '\n' );
  235.       }
  236.     else
  237.       return( c );
  238.  
  239.   return( getchar() );
  240.   }
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249. /****************************************************************/
  250. /*                                */
  251. /*    Get_Base( character )                    */
  252. /*                                */
  253. /*        Return an appropriate base number for the    */
  254. /*        given character code. Used by 'i' and 'o'.    */
  255. /*                                */
  256. /****************************************************************/
  257.  
  258.  
  259. int Get_Base( code )
  260.   char code;
  261.  
  262.   {
  263.   switch ( code )
  264.     {
  265.     case 'h' :        return( HEXADECIMAL );
  266.  
  267.     case 'd' :        return( DECIMAL     );
  268.  
  269.     case 'o' :        return( OCTAL       );
  270.  
  271.     case 'b' :        return( BINARY      );
  272.  
  273.     case 'a' :      return( ASCII       );
  274.  
  275.     default  :        return( ERROR       );
  276.     }
  277.   }
  278.